package com.atguigu.ssm.controller;

import com.atguigu.ssm.pojo.Employee;
import com.atguigu.ssm.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author 31427
 * @create 2022/10/9 19:49
 * @description
 * 查询所有的员工信息-->/employee-->get
 * 获取员工的分页信息-->/employee/page/1-->get
 * 根据id查询员工信息-->/employee/1-->get
 * 跳转到添加页面-->/to/add-->get
 * 添加员工信息->/employee-->post
 * 修改员工信息-->/employee/1-->put
 * 查询所有的员工信息-->/employee/1-->delete
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
    没分页之前
    @RequestMapping(value = "/employee",method = RequestMethod.GET)
    public String getAllEmployee(Model model){
        //查询所有员工信息
        List<Employee> list = employeeService.getAllEmployee();
        //将员工信息在请求域中共享
        model.addAttribute("list",list);
        //跳转到employee_list.html
        return "employee_list";
    }*/


    /**
     * 分页之后
     * @param pageNum
     * @param model
     * @return
     */

    //1、查询所有信息
    @RequestMapping(value = "/employee/page/{pageNum}",method = RequestMethod.GET)
    public String getEmployeePage(@PathVariable Integer pageNum,Model model){
        //获取员工的分页信息
        PageInfo<Employee> page = employeeService.getEmployeePage(pageNum);
        //将分页数据共享到请求域中
        model.addAttribute("page",page);
        return "employee_page_list";
    }
    //2、添加员工
    @RequestMapping(value = "/employee/page/{pageNum}",method = RequestMethod.POST)
    public String addEmployee(Employee employee){
        employeeService.addEmployee(employee);
        return "redirect:/employee/page/{pageNum}";
    }

    //修改员工1:根据id查询员工信息
    @RequestMapping(value = "/employee/page/{pageNum}/{empId}",method = RequestMethod.GET)
    public String updateEmployee (@PathVariable("empId") Integer empId,Model model){
        //根据id查询员工信息
        Employee employee =  employeeService.queryemployeeByEmpId(empId);
        //将员工信息共享到请求域中
        model.addAttribute("employee",employee);
        //跳转到employee_update.html
        return "employee_update";
    }
    //修改员工2:开始修改提交重定向到employeelist页面
    @RequestMapping(value = "/employee/page/{pageNum}",method = RequestMethod.PUT)
    public String updateEmployee1(Employee employee){
        employeeService.updateEmployee(employee);
        return "redirect:/employee/page/{pageNum}";
    }

    //4、删除员工
    @RequestMapping(value = "/employee/page/{pageNum}/{empId}",method = RequestMethod.DELETE)
    public String deleteEmployee (@PathVariable("empId") Integer empId){
        employeeService.deleteEmployee(empId);
        return "redirect:/employee/page/{pageNum}";
    }
}
